package com.bytecodezz.app.sketch.common.base

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.CallSuper
import androidx.viewbinding.ViewBinding
import com.bytecodezz.app.base.extend.ui.onClick
import com.bytecodezz.app.base.framework.base.model.BaseModelFragment
import com.bytecodezz.app.base.framework.tools.StatusBarTools
import com.bytecodezz.app.sketch.common.R

/**
 * @author: bytecodezz
 * @version: 1.0
 * @time: 2021/7/5 18:04
 * @package: com.bytecodezz.app.base
 *
 * @description:业务层级的顶级Fragment基类
 *
 */
abstract class BaseUIFragment<V : ViewBinding, M : BaseUIViewModel> :
    BaseModelFragment<V, M>() {
    protected var titleTv: TextView? = null
    protected var appBarActionTv: TextView? = null
    protected var backImageView: ImageView? = null

    override fun initOtherObserver() {
        super.initOtherObserver()
        viewModel?.let {
            it.isShowProgress.observe(this) { isShowProgress ->
                if (isShowProgress) {
                    //show progress
                    showProgress()

                } else {
                    //dismiss progress
                    dismissProgress()
                }
            }
        }
    }

    fun showProgress(content: String = "加载中", isCancel: Boolean = false) {

    }

    fun dismissProgress() {
    }

    @CallSuper
    override fun onActionStart(contentView: View) {
        titleTv = contentView.findViewById(R.id.app_bar_title_tv)
        appBarActionTv = contentView.findViewById(R.id.app_bar_action_tv)
        backImageView = contentView.findViewById(R.id.app_bar_back_imv)
        backImageView?.onClick {
            onKeyBack()
        }
        appBarActionTv?.onClick {
            onAppBarActionClick()
        }
    }

    open fun onKeyBack() {
    }

    open fun onAppBarActionClick() {
    }

    fun setStatusBarColor(color: Int) {
        StatusBarTools.setStatusBarColor(requireActivity(), color)
    }

    /**
     * 用于保存上一个页面的状态栏颜色
     * 仅在当前Fragment需要修改状态栏，并且在当前Fragment Destroy的时候重新设置为原来颜色 的时候调用
     */
    fun saveStatusBarColor() {
        viewModel?.let {
            if (it.statusBarColor == null) {
                it.statusBarColor = StatusBarTools.getCurrentStatusBarColor()
            }
        }
    }

    fun setStatusBarPadding(rootViewId: Int, isOpenPadding: Boolean = true) {
        StatusBarTools.setOffsetStatusBar(
            requireActivity(),
            rootView?.findViewById(rootViewId),
            isOpenPadding,
            true
        )
    }

    fun setStatusBarMargin(rootViewId: Int, isOpenMargin: Boolean = true) {
        StatusBarTools.setOffsetStatusBar(
            requireActivity(),
            rootView?.findViewById(rootViewId),
            isOpenMargin,
            false
        )
    }

    override fun onDestroy() {
        viewModel?.let {
            it.statusBarColor?.let { color ->
                setStatusBarColor(color)
            }
        }
        super.onDestroy()
    }
}